<?php
/*
write by vomtung@yahoo.com.vn
*/
 class Mobileservice_AnnouncementController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
  }
  public function showallannouncementAction()
  {
  $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$table = Engine_Api::_()->getDbtable('announcements', 'announcement');
    $select = $table->select();
	$paginator = Zend_Paginator::factory($select);
	$first=true;
	 $jsondata = '{  "result":"Success",'.' "Data":[';
	 foreach( $paginator as $item ) {
      if( !empty($item->announcement_id) ) {
	  if($first==true) $first=false;
	  else $jsondata=$jsondata.',';
		$jsondata = $jsondata .'{ "announcement_id":"'.$item->announcement_id.
		'","user_id":"'.$item->user_id.
		'","title":"'.$item->title.
		'","body":"'.$item->body.
		'","creation_date":"'.$item->creation_date.
		'" }';
      }
    }
	$jsondata=$jsondata.' ]}';
	
	echo $jsondata;
  }
  public function postnewannouncementAction()
  {
  $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$title=$_POST["title"];
	$body=$_POST["body"];
	//$title='hahaha';
	//$body='no body';
	$params =array();
	$params['title']=$title;
	$params['body']='<p>'.$body.'</p>';
    $params['user_id'] = '1';
    $announcement = Engine_Api::_()->getDbtable('announcements', 'announcement')->createRow();
    $announcement->setFromArray($params);
    $announcement->save();
	//increment statistics
     Engine_Api::_()->getDbtable('statistics', 'core')->increment('announcement.creations');
	echo json_encode(array("result"=>"PostAnnouncementSuccess"));
  }
  public function editannouncementAction()
  {
  $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$announcement_id=$_POST["announcement_id"];
	$title=$_POST["title"];
	$body=$_POST["body"];
	//$announcement_id='7';
	//$title='hahaha';
	//$body='no body';
	$table = Engine_Api::_()->getDbtable('announcements', 'announcement');
    $select = $table->select();
	$select->where('announcement_id = ?', $announcement_id );
	$paginator = Zend_Paginator::factory($select);
	 foreach( $paginator as $item ) {
		$item->title=$title;
		$item->body=$body;
		$item->save();
    }
	echo json_encode(array("result"=>"Edit Announcement Success"));
  }
  public function getannouncementfromidAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$announcement_id=$_POST["announcement_id"];
	//$announcement_id='2';
	$table = Engine_Api::_()->getDbtable('announcements', 'announcement');
    $select = $table->select();
	$select->where('announcement_id = ?', $announcement_id );
	$paginator = Zend_Paginator::factory($select);
	$first=true;
	 $jsondata = '{  "result":"Success",'.' "Data":[';
	 foreach( $paginator as $item ) {
      if( !empty($item->announcement_id) ) {
	  if($first==true) $first=false;
	  else $jsondata=$jsondata.',';
		$jsondata = $jsondata .'{ "announcement_id":"'.$item->announcement_id.
		'","user_id":"'.$item->user_id.
		'","title":"'.$item->title.
		'","body":"'.$item->body.
		'","creation_date":"'.$item->creation_date.
		'" }';
      }
    }
	$jsondata=$jsondata.' ]}';
	
	echo $jsondata;
  }
  public function deleteannouncementAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$announcement_id=$_POST["announcement_id"];
	$table = Engine_Api::_()->getDbtable('announcements', 'announcement');
	$select = $table->select();
	$select->where('announcement_id = ?', $announcement_id );
	$paginator = Zend_Paginator::factory($select);
	 foreach( $paginator as $item ) {
		$item->delete();
    }
	echo json_encode(array("result"=>"Delete Announcement Success"));
  }
}
 ?>